package com.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Customer;
import com.insurance.domain.entity.Notice;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.NoticePageVo;
import com.insurance.domain.vo.PageVo;
import com.insurance.mapper.NoticeMapper;
import com.insurance.service.NoticeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Notice)表服务实现类
 *
 * @author makejava
 * @since 2023-04-28 19:09:21
 */
@Service("noticeService")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Override
    public ResponseResult getNoticesByPage(NoticePageVo noticePageVo) {
        LambdaQueryWrapper<Notice> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(noticePageVo.getTitle()),Notice::getTitle,noticePageVo.getTitle());
        queryWrapper.eq(!ObjectUtils.isEmpty(noticePageVo.getGrade()),Notice::getGrade,noticePageVo.getGrade());
        queryWrapper.like(!ObjectUtils.isEmpty(noticePageVo.getContent()),Notice::getContent,noticePageVo.getContent());
        Page<Notice> page = new Page<>(noticePageVo.getPageNo(), noticePageVo.getPageSize());
        page(page,queryWrapper);
        List<Notice> nitices = page.getRecords();
        PageVo pageVo = new PageVo(nitices, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addNotice(Notice notice) {
        if(save(notice)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ADD_FAILED);
    }

    @Override
    public ResponseResult updateNotice(Notice notice) {
        if(updateById(notice)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }
}
